﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ChairtyBank.DAL;

namespace ChairtyBank
{
    public partial class LoginForm : Form
    {
        public LoginForm()
        {
            InitializeComponent();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            this.Hide();
            UserForm form = new UserForm();
            form.Show();
            form.loginForm = this;

        }


        private void button1_Click(object sender, EventArgs e)
        {
            loginAction();
        }


        private void loginAction()
        {
            UserEntity entity = fillEntityFromForm();
            if (entity != null && authnticateUser(entity))
            {
                MainWindow form = new MainWindow();
                form.user = User.GetUser(entity.UName);
                form.Show();
                form.loginForm = this;
                this.Hide();
            }        
        }

        private UserEntity fillEntityFromForm()
        {
            if (txtPassword.Text == "" || txtName.Text == "")
            {
                lblerror.Text = "خطـأ .. !!  لابد من أدخال الاسم وكلمة المرور  ";
                return null;
            }
            else
            {
                UserEntity entity = new UserEntity();
                entity.UName = txtName.Text;
                entity.UPassword = txtPassword.Text;
                return entity;
            }
        }

        public void clearFields()
        {
            this.txtName.Text = "";
            this.txtPassword.Text = "";
            this.lblerror.Text = "";
        }

        private void linkLabel1_LinkClicked_1(object sender, LinkLabelLinkClickedEventArgs e)
        {
            UserEntity entity = fillEntityFromForm();
            if (entity != null && authnticateUser(entity))
            {
                UserForm form = new UserForm();
                form.loginForm = this;
                form.exisitingUser = User.GetUser(entity.UName).Id;
                form.Show();
                form.txtName.Text = entity.UName;
            }
        }

        private bool authnticateUser(UserEntity entity)
        {
            bool authorized = true;
            if (entity != null)
            {
                entity = User.GetUser(entity.UName);
                if (entity == null)
                {
                    lblerror.Text = "خطـأ .. !! هذا المستخدم غير موجود";
                    authorized = false;
                }
                else
                {
                    if (entity.UPassword != txtPassword.Text)
                    {
                        lblerror.Text = "خطـأ .. !! كلمة المرور غير صحيحة";
                        authorized = false;
                    }
                }
            }
            return authorized;
        }

        private void LoginForm_Load(object sender, EventArgs e)
        {

        }

        private void txtPassword_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                loginAction();
            }
        }
    }

}
